package DAO;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

import Main.DBMain;
import View.QueryFrame;

public class QueryByClass extends JFrame {
	private JPanel contentPane = new JPanel();
	private JTextField classNoField = new JTextField();
	public static JTextArea showArea = new JTextArea(100, 100);
	private JLabel lab_Tip;
	private JButton btn_Exit;
	private JButton btn_Query;
	String getClassNo = null;

	public QueryByClass() {
		setTitle("班级成绩");
		setSize(600, 400);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_Tip = new JLabel("请输入班级:");
		lab_Tip.setFont(new Font("宋体", Font.PLAIN, 16));
		;
		lab_Tip.setBounds(30, 10, 150, 40);

		/* 按键监听，回车查询 */
		classNoField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					query();
				}
			}
		});
		/* 鼠标动作监听，点击文本框清空内容 */
		classNoField.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				classNoField.setText("");
			}
		});
		classNoField.setBounds(180, 15, 130, 30);
		btn_Query = new JButton("查询");
		btn_Query.setFont(new Font("宋体", Font.BOLD, 16));
		;
		btn_Query.setBounds(360, 10, 70, 40);
		/* 按钮监听，点击查询 */
		btn_Query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				query();
			}
		});

		showArea.setText("");
		showArea.setBounds(5, 65, 575, 290);

		btn_Exit = new JButton("关闭");
		btn_Exit.setFont(new Font("宋体", Font.BOLD, 16));
		btn_Exit.setBounds(480, 10, 70, 40);
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		contentPane.add(lab_Tip);
		contentPane.add(classNoField);
		contentPane.add(showArea);
		contentPane.add(btn_Query);
		contentPane.add(btn_Exit);

		setVisible(true);
	}

	public void query() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs;
		getClassNo = classNoField.getText().trim();
		showArea.setText("");
		boolean result = false;
		if (!("".equals(getClassNo))) {
			try {
				conn = DBMain.getConnection();
				String sql = "select * from stu_info where classNo= '" + getClassNo + "'";// 查询语句
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery(sql);
				result = classIsExist(getClassNo);
				ResultSetMetaData colName = rs.getMetaData();
				/* 获取查询到的对象 */
				if (result) {
					for (int i = 1; i < colName.getColumnCount(); i++) {
						showArea.append(colName.getColumnName(i) + "\t");
					}
					showArea.append("\n");
					while (rs.next()) {
						for (int i = 1; i < colName.getColumnCount(); i++) {
							showArea.append(rs.getString(i) + "\t");
						}
						showArea.append("\n");
					}
				} else {
					JOptionPane.showMessageDialog(null, "该班级不存在！");
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "请输入班级！");
		}
	}
	//判断一下是否存在该班级
	public boolean classIsExist(String clas) {
		Connection conn1 = null;
		Statement stmt = null;
		ResultSet rs1 = null;
		boolean exist = false;
		try {
			conn1 = DBMain.getConnection();
			stmt = conn1.createStatement();
			rs1 = stmt.executeQuery("select * from stu_info where classNo= '" + getClassNo + "'");// 查询语句
			if (rs1.next()) {
				exist = true;
			} else {
				exist = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return exist;  //返回判断结果
	}
}
